package com.example.demo.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.example.demo.entity.ExamExam;
import com.example.demo.entity.ExamExamDetail;
import com.example.demo.entity.ExamUser;
import com.example.demo.service.impl.ExamExamDetailServiceImpl;
import com.example.demo.service.impl.ExamExamServiceImpl;
import com.example.demo.service.impl.ExamUserServiceImpl;
import lombok.RequiredArgsConstructor;
import org.apache.poi.xwpf.usermodel.*;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletResponse;
import java.awt.*;
import java.io.*;
import java.net.URISyntaxException;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping("/export")
@RequiredArgsConstructor
@Component
public class ExportController extends HttpServlet {
    private final ExamExamDetailServiceImpl examExamDetailService;
    private final ExamExamServiceImpl examExamService;
    private final ExamUserServiceImpl examUserService;
    @GetMapping("/export")
    public List exportDataToWord(Integer id,HttpServletResponse response) throws IOException {
        ExamExamDetail examDetail=examExamDetailService.selectById(id);
        ExamUser examUser1=examUserService.selectById(examDetail.getUserid());
        ExamUser examUser=examUserService.selectByIdCard(examUser1.getIdcard());
        ExamExam examExam=examExamService.selectById(examDetail.getExamid());
        JSONObject json = JSONObject.parseObject(examDetail.getQuestioninfo());
        String totalScore = json.getString("totalScore");
        List<List> list=MakeJsonData(json);
        //合成文档
        //XWPFDocument document=exportDataToWord(list,totalScore, examExam,examDetail);

        //保存文档
//        response.setContentType("application/vnd.openxmlformats-officedocument.wordprocessingml.document");
//        response.setHeader("Content-disposition", "attachment; filename="+examExam.getId()+".docx");
//        OutputStream out = response.getOutputStream();
//        document.write(out);
//        out.flush();
//        out.close();
        // 保存文档
//        FileOutputStream out = new FileOutputStream("output.docx");
//        document.write(out);
//        out.close();
        List info=new ArrayList<>();
        info.add(examUser.getUsername());
        info.add(examUser.getUsersex());
        info.add(examUser.getCompanyname());
        info.add(examUser.getIdcard());
        info.add(examUser.getPhonenumber());
        info.add(examExam.getExamname());
        info.add(totalScore+"分");
        list.add(info);
        return list;

    }
//    public XWPFDocument exportDataToWord(List<List> data, String totalScore, ExamExam examExam, ExamExamDetail examExamDetail) throws IOException {
//        List<String> title=data.get(0);
//        List<String> value=data.get(1);
//        List<String> score=data.get(2);
//        List<String> myScore=data.get(3);
//        XWPFDocument document = new XWPFDocument();
//        XWPFParagraph paragraphTitle = document.createParagraph();
//        XWPFRun run = paragraphTitle.createRun();
//        // 设置段落文本
//        run.setText(examExam.getExamname());
//        run.setFontSize(20);
//        paragraphTitle.setAlignment(ParagraphAlignment.CENTER);
//        run.addBreak();
//
//        XWPFParagraph paragraphName = document.createParagraph();
//        XWPFRun run1 = paragraphName.createRun();
//        run1.setText(examExamDetail.getUsername()+"  "+totalScore+"分");
//        paragraphName.setAlignment(ParagraphAlignment.RIGHT);
//
//        for(int i=0;i<title.size();i++){
//            XWPFParagraph paragraph = document.createParagraph();
//            paragraph.createRun().setText(title.get(i)+"         "+myScore.get(i)+"分/"+score.get(i)+"分");
//            paragraph.setAlignment(ParagraphAlignment.LEFT);
//
//            XWPFParagraph paragraph1 = document.createParagraph();
//            paragraph1.setAlignment(ParagraphAlignment.LEFT);
//            paragraph1.setIndentationFirstLine(400);
//            paragraph1.createRun().setText(value.get(i));
//            paragraph1.createRun().addBreak();
//        }
//        return document;
//    }
    public List<List> MakeJsonData(JSONObject json){
        JSONArray dataArr =JSONArray.parseArray(json.getString("data"));
        List<String> title=new ArrayList<>();
        List<String> value=new ArrayList<>();
        List<String> score=new ArrayList<>();
        List<String> myScore=new ArrayList<>();
        for (int i = 0; i < dataArr.size(); i++) {
            JSONObject data = JSONObject.parseObject(dataArr.get(i).toString());
            title.add(data.getString("name"));
            value.add(data.getString("operationRecords"));
            score.add(data.getString("score"));
            myScore.add(data.getString("myScore"));
        }
        List<List> data=new ArrayList();
        data.add(title);
        data.add(value);
        data.add(score);
        data.add(myScore);
        return data;
    }
}
